<template>
  <div class="dataFusion">
    <div class="upchart"></div>
    <div class="midchart"></div>
  </div>
</template>

<script setup>
import echarts from "@/plugins/echarts";
import { ref, onMounted, computed,defineExpose } from "vue";
import { ScatterChart } from "echarts/charts";
import { BrushComponent } from 'echarts/components';
import { codeFusion } from '@/api/dataProcessing'
import { message } from "@/utils/message";

echarts.use([ScatterChart]);
echarts.use([BrushComponent]);

const fileName = ref("");
const chartYName = ref("储纱机构振动数据")
const fileDataObj = ref('');
const childStatusObj = ref({
  title: '未清洗',
  status: 500
});

const emit = defineEmits(['childTitleStatusClick']);
emit('childTitleStatusClick', childStatusObj.value)

const props = defineProps({
  fileData: {
    type: String,
    default: ''
  },
  fileName: {
    type: String,
    default: ''
  },
  selectValue: {
    type: Object,
    default () {
      return {}
    }
  },
  switchValue: {
    type: Array,
    default () {
      return []
    }
  }
});

fileName.value = props.fileName;
// base64格式转file对象
const base64ToFile = (base64, fileName) => {
  let arr = base64.split(",");
  // let mime = arr[0].match(/:(.\*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let u8arr = new Uint8Array(n);

  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], fileName, { type: '' });
}
fileDataObj.value = base64ToFile(props.fileData, props.fileName)

onMounted(() => {
  getChart();
  getChart1();
});

let myChart = ref();
function getChart() {
  var chart = document.querySelector(".upchart");
  myChart.value = echarts.init(chart);
  var option = {
    title: {
      text: "数据融合前",
      textStyle: {
        fontSize: 15 // 设置字号为 14px
      }
    },
    grid: {
      left: "3%",
      right: "7%",
      bottom: "7%",
      containLabel: true
    },
    legend: {
      right: "15%",
      data: ["电压数据", "电流数据"],
      icon: "roundRect",
      top: "5%",
      textStyle: {
        fontSize: 14
      }
    },
    tooltip: {
      // trigger: 'axis',
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return (
            params.seriesName +
            " :<br/>" +
            params.value[0] +
            " " +
            params.value[1] +
            " "
          );
        } else {
          return (
            params.seriesName +
            " :<br/>" +
            params.name +
            " : " +
            params.value +
            " "
          );
        }
      },
      axisPointer: {
        show: true,
        type: "cross",
        lineStyle: {
          type: "dashed",
          width: 1
        }
      }
    },
    xAxis: [
      {
        type: "category",
        scale: true,
        axisLabel: {
          formatter: "{value} "
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        name: chartYName.value,
        type: "value",
        nameRotate:0.1,
        scale: true,
        axisLabel: {
          formatter: "{value} "
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: "电压数据",
        type: "scatter",
        symbolSize: 5,
        emphasis: {
          focus: "series"
        },
        data: [[161.2, 51.6], [167.5, 59.0]],
      },
      {
        name: "电流数据",
        type: "scatter",
        symbolSize: 5,
        emphasis: {
          focus: "series"
        },
        data: [[171.2, 79.1], [181.6, 78.9]],
      }
    ]
  };
  option && myChart.value.setOption(option);
}

let myChart1 = ref();
function getChart1() {
  var chartDom = document.querySelector(".midchart");
  myChart1.value = echarts.init(chartDom);
  var option = {
    title: {
      text: "数据融合后",
      textStyle: {
      fontSize: 15 // 设置字号为 14px
    }
    },
    grid: {
      left: "3%",
      right: "7%",
      bottom: "7%",
      containLabel: true
    },
    legend: {
      right: "15%",
      data: ["融合后数据"],
      icon: "roundRect",
      top: "5%",
      textStyle: {
        fontSize: 14
      }
    },
    tooltip: {
      // trigger: 'axis',
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return (
            params.seriesName +
            " :<br/>" +
            params.value[0] +
            " " +
            params.value[1] +
            " "
          );
        } else {
          return (
            params.seriesName +
            " :<br/>" +
            params.name +
            " : " +
            params.value +
            " "
          );
        }
      },
      axisPointer: {
        show: true,
        type: "cross",
        lineStyle: {
          type: "dashed",
          width: 1
        }
      }
    },
    xAxis: [
      {
        type: "category",
        scale: true,
        axisLabel: {
          formatter: "{value} "
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        name: chartYName.value,
        type: "value",
        nameRotate:0.1,
        scale: true,
        axisLabel: {
          formatter: "{value} "
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: "融合后数据",
        type: "scatter",
        symbolSize: 5,
        emphasis: {
          focus: "series"
        },
        // prettier-ignore
        data: [[168.3, 54.8], [180.3, 60.7]],
      },
    ]
  };
  option && myChart1.value.setOption(option);
}

  // 开始融合
  const childMethod = () => {
    if (fileDataObj.value) {
      childStatusObj.value.title = '融合中....';
      emit('childTitleStatusClick', childStatusObj.value)
      let formData = new FormData()
      if (props.switchValue[0].value) {
        formData.append("method", props.selectValue.selectA)
      } else {
        formData.append("file", fileDataObj.value)
      }
      formData.append("type", props.selectValue.selectC)

      codeFusion(formData).then(res => {
        if (res.status === 200) {
          myChart.value.setOption({
            series: [
              {
                data: res.data.oridata1
              },
              {
                data: res.data.oridata2
              },
            ]
          });
          myChart1.value.setOption({
            series: [
              {
                data: res.data.prodata
              }]
          });
          if (res.data.prodata.length > 0) {
            childStatusObj.value.title = "融合完成"
            childStatusObj.value.status = res.status;
            emit('childTitleStatusClick', childStatusObj.value)
          }
        } else {
          childStatusObj.value.title = '融合失败';
          childStatusObj.value.status = res.status;
          emit('childTitleStatusClick', childStatusObj.value)
        }
      }).catch(error => {
        childStatusObj.value.title = '融合失败';
        childStatusObj.value.status = error.status;
        emit('childTitleStatusClick', childStatusObj.value)
        console.log(error)
      });
    }
  }
  defineExpose({
    childMethod
  })
    //计算属性
    // const tagType = computed(() => {
    //   if (fusion_res.value ==='未进行数据融合') {
    //     return 'danger'
    //   } else if (fusion_res.value ==='数据融合完成') {
    //     return 'success'
    //   }
    // })
</script>

<style>
  .el-upload-list {
    display: none;
  }
</style>
<style lang="scss" scoped>
.dataFusion {
  .upchart {
    width: 100%;
    height: 252px;
  }
  .midchart {
    width: 100%;
    height: 252px;
  }
}
</style>
